home *** CD-ROM | disk | FTP | other *** search
- package java.beans;
-
- import java.lang.reflect.Method;
-
- public class IndexedPropertyDescriptor extends PropertyDescriptor {
- private Class indexedPropertyType;
- private Method indexedReadMethod;
- private Method indexedWriteMethod;
-
- IndexedPropertyDescriptor(PropertyDescriptor x, PropertyDescriptor y) {
- super(x, y);
- if (x instanceof IndexedPropertyDescriptor) {
- IndexedPropertyDescriptor ix = (IndexedPropertyDescriptor)x;
- this.indexedReadMethod = ix.indexedReadMethod;
- this.indexedWriteMethod = ix.indexedWriteMethod;
- this.indexedPropertyType = ix.indexedPropertyType;
- }
-
- if (y instanceof IndexedPropertyDescriptor) {
- IndexedPropertyDescriptor iy = (IndexedPropertyDescriptor)y;
- if (iy.indexedReadMethod != null) {
- this.indexedReadMethod = iy.indexedReadMethod;
- }
-
- if (iy.indexedWriteMethod != null) {
- this.indexedWriteMethod = iy.indexedWriteMethod;
- }
-
- this.indexedPropertyType = iy.indexedPropertyType;
- }
-
- }
-
- public IndexedPropertyDescriptor(String propertyName, Class beanClass) throws IntrospectionException {
- this(propertyName, beanClass, "get" + capitalize(propertyName), "set" + capitalize(propertyName), "get" + capitalize(propertyName), "set" + capitalize(propertyName));
- }
-
- public IndexedPropertyDescriptor(String propertyName, Class beanClass, String getterName, String setterName, String indexedGetterName, String indexedSetterName) throws IntrospectionException {
- super(propertyName, beanClass, getterName, setterName);
- this.indexedReadMethod = Introspector.findMethod(beanClass, indexedGetterName, 1);
- this.indexedWriteMethod = Introspector.findMethod(beanClass, indexedSetterName, 2);
- this.findIndexedPropertyType();
- }
-
- public IndexedPropertyDescriptor(String propertyName, Method getter, Method setter, Method indexedGetter, Method indexedSetter) throws IntrospectionException {
- super(propertyName, getter, setter);
- this.indexedReadMethod = indexedGetter;
- this.indexedWriteMethod = indexedSetter;
- this.findIndexedPropertyType();
- }
-
- private static String capitalize(String s) {
- char[] chars = s.toCharArray();
- chars[0] = Character.toUpperCase(chars[0]);
- return new String(chars);
- }
-
- private void findIndexedPropertyType() throws IntrospectionException {
- try {
- this.indexedPropertyType = null;
- if (this.indexedReadMethod != null) {
- Class[] params = this.indexedReadMethod.getParameterTypes();
- if (params.length != 1) {
- throw new IntrospectionException("bad indexed read method arg count");
- }
-
- if (params[0] != Integer.TYPE) {
- throw new IntrospectionException("non int index to indexed read method");
- }
-
- this.indexedPropertyType = this.indexedReadMethod.getReturnType();
- if (this.indexedPropertyType == Void.TYPE) {
- throw new IntrospectionException("indexed read method returns void");
- }
- }
-
- if (this.indexedWriteMethod != null) {
- Class[] params = this.indexedWriteMethod.getParameterTypes();
- if (params.length != 2) {
- throw new IntrospectionException("bad indexed write method arg count");
- }
-
- if (params[0] != Integer.TYPE) {
- throw new IntrospectionException("non int index to indexed write method");
- }
-
- if (this.indexedPropertyType != null && this.indexedPropertyType != params[1]) {
- throw new IntrospectionException("type mismatch between indexed read and indexed write methods");
- }
-
- this.indexedPropertyType = params[1];
- }
-
- if (this.indexedPropertyType == null) {
- throw new IntrospectionException("no indexed getter or setter");
- } else {
- Class propertyType = ((PropertyDescriptor)this).getPropertyType();
- if (propertyType != null && (!propertyType.isArray() || propertyType.getComponentType() != this.indexedPropertyType)) {
- throw new IntrospectionException("type mismatch between indexed and non-indexed methods");
- }
- }
- } catch (IntrospectionException var2) {
- throw var2;
- }
- }
-
- public Class getIndexedPropertyType() {
- return this.indexedPropertyType;
- }
-
- public Method getIndexedReadMethod() {
- return this.indexedReadMethod;
- }
-
- public Method getIndexedWriteMethod() {
- return this.indexedWriteMethod;
- }
- }
-